package org.com.ashen.service.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;

import javax.imageio.ImageIO;
 
public class ImgUtil {
	public static void main(String[] args) throws Exception {
		String srcImgPath = "/Users/longsheng/Desktop/00.jpg";
		String iconPath = "/Users/longsheng/HBuilderProjects/dinghuo/static/images/address.png";
//		String imgurl = "https://www.a-shen.net/files/NBMT/SP/20210414165424756_SP_d.jpg";
//		String serverPath = "/Users/longsheng/Desktop/";
//		String subFixStr =  imgurl.substring(imgurl.lastIndexOf(".")+1);
//		String picname = UUID.randomUUID().toString()+ "."+subFixStr;
//		String relativePath2 = picname;
//		String dirPath2 = serverPath+ relativePath2;
//        URL url = new URL(imgurl);
//        URLConnection connection = url.openConnection();
//        InputStream is = connection.getInputStream();
//        File file2 = new File(dirPath2);
//        if (!file2.exists()){
//            file2.createNewFile();
//        }
//        OutputStream out = new FileOutputStream(file2);
//        int len2 = 0;
//        byte[] buf2 = new byte[1024];
//        while ((len2 = is.read(buf2, 0, 1024)) != -1) {
//        	out.write(buf2, 0, len2);
//        }
//        out.flush();
//        if(is != null){
//            try {
//                is.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        if(out != null){
//            try {
//            	out.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        srcImgPath = dirPath2;
        
		pressImage(srcImgPath, iconPath,"","");
	}
	
	
	public static boolean pressImage(String targetImg, String waterImg,String goods_title,String goods_price) {
		try {
			String goods_p = "长按识别小程序码识别";
			int fontSize = 35;
			File file = new File(targetImg);
			Image image = ImageIO.read(file);
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			BufferedImage bufferedImage = new BufferedImage(width, height+200,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.setColor(Color.WHITE);
			g.fillRect(0,0,width,height+200);
			g.drawImage(image, 0, 0, width, height, null);
			Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件
			int width_1 = 145;
			int height_1 = 145;
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
					1.0f));
			int widthDiff = width - width_1-20;
			int heightDiff = height +180 - height_1;
			int x = widthDiff ;// 水印位置
			int y = heightDiff ;// 水印位置
			g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束
			
			g.setFont(new Font("宋体", Font.PLAIN, fontSize));
			g.setColor(Color.BLACK);
			 x = 10;//水印位置
			 y = height+50;
			g.drawString(goods_title, x, y );
			
			 x = 10;//水印位置
			 y = height+130;
			width_1 = 45 * getLength("箱价：");
			g.setFont(new Font("宋体", Font.BOLD, 45));
			g.setColor(Color.BLACK);
			g.drawString("箱价：", x, y );
			
			g.setFont(new Font("宋体", Font.BOLD, 45));
			g.setColor(new Color(209,24,26));
			g.drawString(goods_price, x+width_1, y );
			
			width_1 = 45*getLength("箱价："+goods_price);
			g.setFont(new Font("宋体", Font.BOLD, 25));
			g.setColor(new Color(209,24,26));
			g.drawString("起", x+width_1+10, y );
			
			
			g.setFont(new Font("宋体", Font.PLAIN, 25));
			g.setColor(Color.GRAY);
			 x = 10;//水印位置
			 y = height+180;
			g.drawString(goods_p, x, y );
			
			g.dispose();
			ImageIO.write(bufferedImage, "png", file);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 照片添加水印
	 * @param targetImg 原始照片路径
	 * @param pressText 水印文字
	 * @param fontSize 文字大小 <=0时字体大小自适应
	 * @param color 文字颜色
	 * @return
	 */
	public static Boolean pressText(String targetImg, String pressText, int fontSize, Color color) {
		try {
			File file = new File(targetImg);
 
			Image image = ImageIO.read(file);
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			BufferedImage bufferedImage = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(image, 0, 0, width, height, null);
			if (fontSize<=0) {
				fontSize = 4*width/(5*pressText.length());
			}
			g.setFont(new Font("宋体", Font.PLAIN, fontSize));
			g.setColor(color);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
					0.5f));
			int width_1 = fontSize * getLength(pressText);
			int height_1 = fontSize;
			int widthDiff = width - width_1;
			int heightDiff = height - height_1;
			int x = widthDiff / 2;//水印位置
			int y = heightDiff / 2;
			g.drawString(pressText, x, y + height_1);
			g.dispose();
			ImageIO.write(bufferedImage, "jpg", file);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
 
	public static int getLength(String text) {
		int textLength = text.length();
		int length = textLength;
		for (int i = 0; i < textLength; i++) {
			if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
				length++;
			}
		}
		return (length % 2 == 0) ? length / 2 : length / 2 + 1;
	}
}